﻿
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UIMissionItemData : MonoBehaviour
{
    public static UIMissionItemData instance;

    public MissionInfo missionInfo;

    public TextMeshProUGUI missionName;

    public TextMeshProUGUI missionDesc;

    private int targetNum;
    private int finishNum;

    public TextMeshProUGUI taskListText;
    public TextMeshProUGUI progress;

    public GameObject revardViewPrefab;
    public Transform revardViewSeat;

    public Button track;
    public Text trackText;
    public Button finish;
    private void Awake()
    {
        instance = this;
        track.gameObject.SetActive(false);
        finish.gameObject.SetActive(false);
        ShowMission();
    }
    private void OnDestroy()
    {
        instance = null;
    }

    public void ShowMission()
    {
        if (missionInfo == null)
        {
            missionName.text = "";
            missionDesc.text = "";
            taskListText.text = "";
            progress.text = "";
            finish.gameObject.SetActive(false);
            if (revardViewSeat.childCount > 1)
            {
                Destroy(revardViewSeat.GetChild(1).gameObject);
            }
            return;
        }
        else
        {
            missionName.text = missionInfo.name;

            if (missionInfo.type == 1)
            {
                RoleInfo target = StaticData.GetRoleInfo(missionInfo.value1);
                missionDesc.text = $"击杀{missionInfo.num1}只{target.name}";
                taskListText.text = $"已击杀{target.name}";
            }
            else if (missionInfo.type == 2)
            {
                ItemInfo target = StaticData.GetItemInfo(missionInfo.value1);
                missionDesc.text = $"寻找{target.name},至少需要{missionInfo.num1}个!";
                taskListText.text = $"已找到{target.name}";
            }
            else if (missionInfo.type == 3)
            {
                missionDesc.text = $"通过地牢第{missionInfo.num1}层";
                taskListText.text = $"已到达";
            }
            progress.text = $"{finishNum} / {missionInfo.num1}";
            if (revardViewSeat.childCount < 2)
            {
                for (int i = 0; i < missionInfo.rewards.Length; i++)
                {
                    GameObject obj = Instantiate(revardViewPrefab, revardViewSeat);
                    obj.GetComponent<UIMissionItemRender>().SetData(missionInfo.rewards[0].type);
                }
            }

            MissionData missionData = GameData.GetMissionData(missionInfo.id);
            finishNum = missionData.progress;
            targetNum = missionInfo.num1;
            if (finishNum >= targetNum)
            {
                track.gameObject.SetActive(false);
                finish.gameObject.SetActive(true);
            }
            else 
            {
                track.gameObject.SetActive(true);
                finish.gameObject.SetActive(false);
            }
        }

        trackText.text = UITrackMissionRender.instance.isTrack && UITrackMissionRender.instance.missionInfo == missionInfo
         ? "取消追踪" : "追踪任务";

    }

    public void OnEnable()
    {
    }

    public void TrackMission()
    {
        if (UITrackMissionRender.instance.isTrack && UITrackMissionRender.instance.missionInfo == missionInfo)
        {
            UITrackMissionRender.instance.isTrack = false;
        }
        else
        {
            UITrackMissionRender.instance.isTrack = true;
            UITrackMissionRender.instance.missionInfo = missionInfo;
        }

        UITrackMissionRender.instance.TrackMission();
    }

    public void FinishMission()
    {
        GameData.FinishMission(missionInfo.id);
        if (UITrackMissionRender.instance.isTrack && UITrackMissionRender.instance.missionInfo == missionInfo)
        {
            UITrackMissionRender.instance.isTrack = false;
            UITrackMissionRender.instance.TrackMission();
        }
        missionInfo = null;
        ShowMission();
        GameController.instance.isGetMission = true;
    }

}

